<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Log;

//重庆渝电内网接口
class CqydApiController extends Controller
{
    private $AppKey = "B9LCFP8T";
    private $AppSecret = "2500b91d82922ffecd7560a65cd442830bfc4d9";
//    private $BaseUrl = "https://mes-tool-dev.tunnel.shunjiantech.cn";
//    private $BaseUrl = "https://mes-tool-cq-dev.tunnel.shunjiantech.cn";
    private $BaseUrl = "http://10.186.247.101:80";
    // private $BaseUrl = "http://10.187.114.51:80";

    private function getUnixTimestamp() {
        list($s1, $s2) = explode(' ', microtime());
        return (int)sprintf('%.0f', (floatval($s1) + floatval($s2))*1000);
    }

    //获取工器具试验项目
    public function getItems($qrcode) {
        $verb = 'GET';
        $path = '/api/v1/open/test_items';
        $query = 'qrcode='.$qrcode;
        list($sign, $time) = $this->getSign($verb, $path, $query);
        $url = $this->BaseUrl.$path.'?'.$query;
        return curl($url, '', ["X-AppKey:{$this->AppKey}", "X-Timestamp:{$time}", "X-Signature:{$sign}"]);
    }

    /**
     * 添加工器具试验
    Body:
    名称  类型  是否必须    默认值 备注  其他信息
    test_item_code  string  必须  试验项目code
    qrcode  string 必须 试验设备二维码
    test_conclusion string 必须 试验结论 只有三种 符合/不符合/不作判定
    test_result string 非必须 检测结果
    extra_param object [] 非必须 额外数据 item 类型: object
     */
    public function addItems($params) {
        $verb = 'POST';
        $path = '/api/v1/open/test_items';
//        $querySign = $this->signQuery($params);
        $body = json_encode($params,JSON_UNESCAPED_UNICODE);
        Log::info("上传内网数据：");
        Log::info($body);
//        dd($body);
//        $body = '{"qrcode":"19974744874942464","test_item_code":"fzxjyst_gpnysy_gy","test_conclusion":"符合","test_result":"没问题","extra_param":[{"voltage":"4kv","duration":"1min"}]}';
        $Content_MD5 = base64_encode(md5($body, true));
        list($sign, $time) = $this->getSign($verb, $path, '','application/json',$Content_MD5);
        $url = $this->BaseUrl.$path;
        return curl($url, $body, ["Content-Type:application/json","Accept: application/json","Content_MD5:{$Content_MD5}","X-AppKey:{$this->AppKey}", "X-Timestamp:{$time}", "X-Signature:{$sign}"]);
    }
    private function signQuery($param){
        ksort($param);
        $str = '';
        foreach($param as $k => $value){
            $str .= $k.'='.$value.'&';
        }
        return trim($str,'&');
    }

    public function getSign($verb, $Path, $Query, $Content_Type = '', $Content_MD5 = '') {
        $TimeStamp = $this->getUnixTimestamp();
        $StringToSign = $verb."\n".
                        $Path."\n".
                        $Query."\n".
                        $Content_Type."\n".
                        $Content_MD5."\n".
                        $TimeStamp."\n".
                        $this->AppSecret;
//        dd($StringToSign);
//        return $StringToSign;
        $HMACSHA1 = hash_hmac('sha1', utf8_encode($StringToSign), $this->AppKey,true);
        $Signature = base64_encode($HMACSHA1);
        return [$Signature, $TimeStamp];
    }

}
